/*****************************************************************************************
 *
 * FILE NAME          : MxL603_OEM_Drv.c
 * 
 * AUTHOR             : Mahendra Kondur
 *
 * DATE CREATED       : 12/23/2011  
 *
 * DESCRIPTION        : This file contains I2C driver and Sleep functins that 
 *                      OEM should implement for MxL603 APIs
 *                             
 *****************************************************************************************
 *                Copyright (c) 2011, MaxLinear, Inc.
 ****************************************************************************************/


#include <sys_config.h>
#include <retcode.h>
#include <types.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <bus/i2c/i2c.h>
#include <osal/osal.h>
#include <hld/nim/nim_dev.h>
#include <hld/nim/nim_tuner.h>

#include "MxL603_TunerApi.h"
#include "MxL603_TunerCfg.h"
#include "tun_mxl603.h"

#if ((SYS_TUN_MODULE == MXL603) || (SYS_TUN_MODULE == ANY_TUNER))

#define TUN_MXL603_PRINTF(...)

static MxL_ERR_MSG MxL603_Tuner_RFTune(UINT32 tuner_id, UINT32 RF_Freq_Hz, MXL603_BW_E BWMHz);
static MxL_ERR_MSG MxL603_Tuner_Init(UINT32 tuner_id);
static BOOL run_on_through_mode(UINT32 tuner_id);
static INT32 tun_mxl603_init_common(UINT32 *tuner_id, struct COFDM_TUNER_CONFIG_EXT * ptrTuner_Config);

typedef struct
{
    MXL603_SIGNAL_MODE_E signalMode;
    MXL_BOOL singleSupply_3_3V;
    UINT8 xtalCap;
    MXL_BOOL clkOutEnable;           //enable or disable clock out.
} MXL603_SPECIAL_CONFIG;

static struct COFDM_TUNER_CONFIG_EXT MxL603_Config[MAX_TUNER_SUPPORT_NUM];
static DEM_WRITE_READ_TUNER m_ThroughMode[MAX_TUNER_SUPPORT_NUM];
static MXL603_SPECIAL_CONFIG m_SpecialConfig[MAX_TUNER_SUPPORT_NUM];
static BOOL bMxl_Tuner_Inited[2];
static UINT32 tuner_cnt = 0;

/*----------------------------------------------------------------------------------------
--| FUNCTION NAME : MxLWare603_OEM_WriteRegister
--| 
--| AUTHOR        : Brenndon Lee
--|
--| DATE CREATED  : 7/30/2009
--|
--| DESCRIPTION   : This function does I2C write operation.
--|
--| RETURN VALUE  : True or False
--|
--|-------------------------------------------------------------------------------------*/

MXL_STATUS MxLWare603_OEM_WriteRegister(UINT32 tuner_id, UINT8 RegAddr, UINT8 RegData)
{
    UINT32 status = ERR_FAILUE;
	UINT8 Cmd[2];
	Cmd[0] = RegAddr;
	Cmd[1] = RegData;

    if (run_on_through_mode(tuner_id))
	{
        status = m_ThroughMode[tuner_id].Dem_Write_Read_Tuner(m_ThroughMode[tuner_id].nim_dev_priv, MxL603_Config[tuner_id].cTuner_Base_Addr, Cmd, 2, 0, 0);
	}
    else
	{
        status = i2c_write(MxL603_Config[tuner_id].i2c_type_id, MxL603_Config[tuner_id].cTuner_Base_Addr, &Cmd, 2);
	}
  
    return (status==SUCCESS ? MXL_TRUE : MXL_FALSE);
}

/*------------------------------------------------------------------------------
--| FUNCTION NAME : MxLWare603_OEM_ReadRegister
--| 
--| AUTHOR        : Brenndon Lee
--|
--| DATE CREATED  : 7/30/2009
--|
--| DESCRIPTION   : This function does I2C read operation.
--|
--| RETURN VALUE  : True or False
--|
--|---------------------------------------------------------------------------*/

MXL_STATUS MxLWare603_OEM_ReadRegister(UINT32 tuner_id, UINT8 RegAddr, UINT8 *DataPtr)
{
    UINT32 status = ERR_FAILUE;
	UINT8 Read_Cmd[2];

	/* read step 1. accroding to mxl301 driver API user guide. */
	Read_Cmd[0] = 0xFB;
	Read_Cmd[1] = RegAddr;
	if (run_on_through_mode(tuner_id))
    {
        status = m_ThroughMode[tuner_id].Dem_Write_Read_Tuner(m_ThroughMode[tuner_id].nim_dev_priv, MxL603_Config[tuner_id].cTuner_Base_Addr, Read_Cmd, 2, 0, 0);
        if(status != SUCCESS)
		{
            return MXL_FALSE;
		}
        status = m_ThroughMode[tuner_id].Dem_Write_Read_Tuner(m_ThroughMode[tuner_id].nim_dev_priv, MxL603_Config[tuner_id].cTuner_Base_Addr, 0, 0, DataPtr, 1);
        if(status != SUCCESS)
		{
            return MXL_FALSE;
		}
	}
    else
    {
    	status = i2c_write(MxL603_Config[tuner_id].i2c_type_id, MxL603_Config[tuner_id].cTuner_Base_Addr, Read_Cmd, 2);
        if(status != SUCCESS)
		{
            return MXL_FALSE;
		}
    	status = i2c_read(MxL603_Config[tuner_id].i2c_type_id, MxL603_Config[tuner_id].cTuner_Base_Addr, DataPtr, 1);
        if(status != SUCCESS)
		{
            return MXL_FALSE;
        }
	}
    
	return MXL_TRUE;
  
  return status;
}

/*------------------------------------------------------------------------------
--| FUNCTION NAME : MxLWare603_OEM_Sleep
--| 
--| AUTHOR        : Dong Liu
--|
--| DATE CREATED  : 01/10/2010
--|
--| DESCRIPTION   : This function complete sleep operation. WaitTime is in ms unit
--|
--| RETURN VALUE  : None
--|
--|-------------------------------------------------------------------------------------*/
void MxLWare603_OEM_Sleep(UINT16 DelayTimeInMs)
{
    osal_task_sleep(DelayTimeInMs);
}



static BOOL run_on_through_mode(UINT32 tuner_id)
{
    return (m_ThroughMode[tuner_id].nim_dev_priv && m_ThroughMode[tuner_id].Dem_Write_Read_Tuner);
}

static INT32 set_through_mode(UINT32 tuner_id, DEM_WRITE_READ_TUNER *ThroughMode)
{
	if(tuner_id >= tuner_cnt)
    {
		return ERR_FAILUE;
    } 
  	MEMCPY((UINT8*)(&m_ThroughMode[tuner_id]), (UINT8*)ThroughMode, sizeof(DEM_WRITE_READ_TUNER));
	return SUCCESS;
}



/*****************************************************************************
* INT32 tun_mxl603_init(struct COFDM_TUNER_CONFIG_EXT * ptrTuner_Config)
*
* Tuner mxl603 Initialization
*
* Arguments:
*  Parameter1: struct COFDM_TUNER_CONFIG_EXT * ptrTuner_Config		: pointer for Tuner configuration structure 
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 tun_mxl603_init(UINT32 *tuner_id, struct COFDM_TUNER_CONFIG_EXT * ptrTuner_Config)
{
	if ((ptrTuner_Config == NULL))
		return ERR_FAILUE;

	if(tuner_cnt >= MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;

    m_SpecialConfig[tuner_cnt].signalMode = MXL603_DIG_DVB_T_DTMB;
    m_SpecialConfig[tuner_cnt].singleSupply_3_3V = MXL_ENABLE;
    m_SpecialConfig[tuner_cnt].xtalCap = 20;
    m_SpecialConfig[tuner_cnt].clkOutEnable = MXL_DISABLE;
    return tun_mxl603_init_common(tuner_id, ptrTuner_Config);
}

INT32 tun_mxl603_init_ISDBT(UINT32 *tuner_id, struct COFDM_TUNER_CONFIG_EXT * ptrTuner_Config)   //ISDB-T mode
{
	if ((ptrTuner_Config == NULL))
		return ERR_FAILUE;

	if(tuner_cnt >= MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;

    m_SpecialConfig[tuner_cnt].signalMode = MXL603_DIG_ISDBT_ATSC;
    m_SpecialConfig[tuner_cnt].singleSupply_3_3V = MXL_ENABLE;
    m_SpecialConfig[tuner_cnt].xtalCap = 20;
    m_SpecialConfig[tuner_cnt].clkOutEnable = MXL_DISABLE;
    return tun_mxl603_init_common(tuner_id, ptrTuner_Config);
}

INT32 tun_mxl603_init_CXD2834(UINT32 *tuner_id, struct COFDM_TUNER_CONFIG_EXT * ptrTuner_Config)    //for CDT full nim: CXD2834+MXL603.
{
	if ((ptrTuner_Config == NULL))
		return ERR_FAILUE;

	if(tuner_cnt >= MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;

    m_SpecialConfig[tuner_cnt].signalMode = MXL603_DIG_DVB_T_DTMB;
    m_SpecialConfig[tuner_cnt].singleSupply_3_3V = MXL_DISABLE;
    m_SpecialConfig[tuner_cnt].xtalCap = 24;
    m_SpecialConfig[tuner_cnt].clkOutEnable = MXL_DISABLE;
    return tun_mxl603_init_common(tuner_id, ptrTuner_Config);
}

INT32 tun_mxl603_init_CDT_MN88472(UINT32 *tuner_id, struct COFDM_TUNER_CONFIG_EXT * ptrTuner_Config)    //for CDT full nim: MN88472+MXL603.
{
	if ((ptrTuner_Config == NULL))
		return ERR_FAILUE;

	if(tuner_cnt >= MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;

    m_SpecialConfig[tuner_cnt].signalMode = MXL603_DIG_DVB_T_DTMB;
    m_SpecialConfig[tuner_cnt].singleSupply_3_3V = MXL_DISABLE;
    m_SpecialConfig[tuner_cnt].xtalCap = 24; //28
    m_SpecialConfig[tuner_cnt].clkOutEnable = MXL_ENABLE;
    return tun_mxl603_init_common(tuner_id, ptrTuner_Config);
}
static INT32 tun_mxl603_init_common(UINT32 *tuner_id, struct COFDM_TUNER_CONFIG_EXT * ptrTuner_Config)
{
    UINT16 Xtal_Freq;
	/* check Tuner Configuration structure is available or not */
	if ((ptrTuner_Config == NULL))
		return ERR_FAILUE;

	if(tuner_cnt >= MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;

	MEMCPY(&MxL603_Config[tuner_cnt], ptrTuner_Config, sizeof(struct COFDM_TUNER_CONFIG_EXT));
    m_ThroughMode[tuner_cnt].Dem_Write_Read_Tuner = NULL;

   	Xtal_Freq = MxL603_Config[tuner_cnt].cTuner_Crystal;
    if(Xtal_Freq >= 1000) //If it's in Khz, then trans it into Mhz.
		Xtal_Freq /= 1000;
    switch (Xtal_Freq)
    {
        case 16:
            MxL603_Config[tuner_cnt].cTuner_Crystal = (MXL603_XTAL_FREQ_E)MXL603_XTAL_16MHz;
            break;
        case 24:
            MxL603_Config[tuner_cnt].cTuner_Crystal = (MXL603_XTAL_FREQ_E)MXL603_XTAL_24MHz;
            break;
		default:
			return ERR_FAILUE;
	}
    
    switch (MxL603_Config[tuner_cnt].wTuner_IF_Freq)
    {
        case 5000:
            MxL603_Config[tuner_cnt].wTuner_IF_Freq = MXL603_IF_5MHz;
            break;
        case 4570:
            MxL603_Config[tuner_cnt].wTuner_IF_Freq = MXL603_IF_4_57MHz;
            break;
		default:
			return ERR_FAILUE;
	}

	bMxl_Tuner_Inited[tuner_cnt] = FALSE;
//	if( MxL603_Tuner_Init(tuner_cnt) != MxL_OK )
//		return ERR_FAILUE;
//	bMxl_Tuner_Inited[tuner_cnt] = TRUE;
   
    if(tuner_id)
	{
	    *tuner_id = tuner_cnt;
	}
	tuner_cnt++;
		
	return SUCCESS;
}

/*****************************************************************************
* INT32 tun_mxl603_status(UINT8 *lock)
*
* Tuner read operation
*
* Arguments:
*  Parameter1: UINT8 *lock		: Phase lock status
*
* Return Value: INT32			: Result
*****************************************************************************/

INT32 tun_mxl603_status(UINT32 tuner_id, UINT8 *lock)
{
    MXL_STATUS status; 
    MXL_BOOL refLockPtr = MXL_UNLOCKED;
    MXL_BOOL rfLockPtr = MXL_UNLOCKED;		

	if(tuner_id >= tuner_cnt)
    {
		return ERR_FAILUE;
    }
	
	*lock = FALSE;   
    status = MxLWare603_API_ReqTunerLockStatus(tuner_id, &rfLockPtr, &refLockPtr);
    if (status == MXL_TRUE)
    {
        if (MXL_LOCKED == rfLockPtr && MXL_LOCKED == refLockPtr)
		{
            *lock  = TRUE;
		}
    }

	return SUCCESS;
}

INT32 tun_mxl603_get_rf_level(UINT32 tuner_id, UINT8 *rf_level)// return level in dbuV.
{
	INT32 result;

	MXL_STATUS status; 
	UINT8 devId;

	INT32 rf_info = 0;		
    REAL32 rxPower = 0;
	devId = (UINT8)(tuner_id & 0xff);
	
	// Read back Tuner RF level.
	status = MxLWare603_API_ReqTunerRxPower(devId, &rxPower);
    TUN_MXL603_PRINTF(" Rx power = %f times of 0.01dBm \n", rxPower);
	if (status == MXL_TRUE)
	{
        rxPower /= 100;
        rf_info = rxPower;
        rf_info += 107; //You can adjust this value for OSD.
		TUN_MXL603_PRINTF("mxl603 get RF level: %d dbm !\n", rf_info);
		//*rf_level = (UINT32)(rf_info + 107); // shift from dbm to dbuV.


		if (rf_info > 255)
		{
			*rf_level = 255;
		}
		else if (rf_info < 0)
		{
			*rf_level = 0;
		}
		else
		{
			*rf_level = rf_info;
		}
		
		TUN_MXL603_PRINTF("mxl603 get RF level: %d dbuV !\n", *rf_level);
			
	}
	else
	{
		*rf_level = 0;
		return ERR_FAILUE;
	}
	

	return SUCCESS;
}

/*****************************************************************************
* INT32 nim_mxl603_control(UINT32 freq, UINT8 bandwidth,UINT8 AGC_Time_Const,UINT8 *data,UINT8 _i2c_cmd)
*
* Tuner write operation
*
* Arguments:
*  Parameter1: UINT32 freq		: Synthesiser programmable divider
*  Parameter2: UINT8 bandwidth		: channel bandwidth
*  Parameter3: UINT8 AGC_Time_Const	: AGC time constant
*  Parameter4: UINT8 *data		: 
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 tun_mxl603_control(UINT32 tuner_id, UINT32 freq, UINT8 bandwidth,UINT8 AGC_Time_Const,UINT8 *data,UINT8 _i2c_cmd)	
{
	MXL603_BW_E BW;
	MxL_ERR_MSG Status;
	
	if(tuner_id >= tuner_cnt)
		return ERR_FAILUE;
	
	switch(bandwidth)
	{
		case 6:
            BW = MXL603_TERR_BW_6MHz;
            break;
		case 7:
            BW = MXL603_TERR_BW_7MHz;
            break;
		case 8:
            BW = MXL603_TERR_BW_8MHz;
            break;
		default:
			return ERR_FAILUE;
	}
    
//	switch(BW)
//	{
//		case MXL603_TERR_BW_6MHz:
//            Mxl301_TunerConfig[tuner_id].IF_Freq = MxL_IF_4_5_MHZ;
//            break;
//		case MXL603_TERR_BW_7MHz:
//            Mxl301_TunerConfig[tuner_id].IF_Freq = MxL_IF_4_5_MHZ;
//            break;
//		case MXL603_TERR_BW_8MHz:
//            Mxl301_TunerConfig[tuner_id].IF_Freq = MxL_IF_5_MHZ;
//            break;
//		default:
//			return ERR_FAILUE;
//	}

	if( bMxl_Tuner_Inited[tuner_id] != TRUE )
	{
    	if( MxL603_Tuner_Init(tuner_id) != MxL_OK )
    	{
    		return ERR_FAILUE;
    	}
		bMxl_Tuner_Inited[tuner_id] = TRUE;
	}

	if(  (Status = MxL603_Tuner_RFTune(tuner_id, freq*1000, BW)) != MxL_OK )
	{
		return ERR_FAILUE;
	}

	return SUCCESS;	
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//																		   //
//							Tuner Functions								   //
//																		   //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
static MxL_ERR_MSG MxL603_Tuner_Init(UINT32 tuner_id)
{	
    MXL_STATUS status; 
    MXL_BOOL singleSupply_3_3V;
    MXL603_XTAL_SET_CFG_T xtalCfg;
    MXL603_IF_OUT_CFG_T ifOutCfg;
    MXL603_AGC_CFG_T agcCfg;
    MXL603_TUNER_MODE_CFG_T tunerModeCfg;
    MXL603_CHAN_TUNE_CFG_T chanTuneCfg;

    MXL603_IF_FREQ_E ifOutFreq;

    //Step 1 : Soft Reset MxL603
    status = MxLWare603_API_CfgDevSoftReset(tuner_id);
    if (status != MXL_SUCCESS)
    {
        TUN_MXL603_PRINTF("Error! MxLWare603_API_CfgDevSoftReset\n");    
        return status;
    }

    //Step 2 : Overwrite Default. It should be called after MxLWare603_API_CfgDevSoftReset and MxLWare603_API_CfgDevXtal.
//    singleSupply_3_3V = MXL_DISABLE;
    singleSupply_3_3V = m_SpecialConfig[tuner_id].singleSupply_3_3V;
    status = MxLWare603_API_CfgDevOverwriteDefaults(tuner_id, singleSupply_3_3V);
    if (status != MXL_SUCCESS)
    {
        TUN_MXL603_PRINTF("Error! MxLWare603_API_CfgDevOverwriteDefaults\n");    
        return status;
    }

    //Step 3 : XTAL Setting
    xtalCfg.xtalFreqSel = MxL603_Config[tuner_id].cTuner_Crystal;
    xtalCfg.xtalCap = m_SpecialConfig[tuner_id].xtalCap; //20, 12
    xtalCfg.clkOutEnable = m_SpecialConfig[tuner_id].clkOutEnable;
    xtalCfg.clkOutDiv = MXL_DISABLE;
    xtalCfg.clkOutExt = MXL_DISABLE;
    xtalCfg.singleSupply_3_3V = singleSupply_3_3V;
    xtalCfg.XtalSharingMode = MXL_DISABLE;
    status = MxLWare603_API_CfgDevXtal(tuner_id, xtalCfg);
    if (status != MXL_SUCCESS)
    {
        TUN_MXL603_PRINTF("Error! MxLWare603_API_CfgDevXtal\n");    
        return status;
    }

    //Step 4 : IF Out setting
//    ifOutCfg.ifOutFreq = MXL603_IF_4_57MHz; //MXL603_IF_4_1MHz
//    ifOutCfg.ifOutFreq = MXL603_IF_5MHz;    //For match to DEM mn88472 IF: DMD_E_IF_5000KHZ.
    ifOutCfg.ifOutFreq = MxL603_Config[tuner_id].wTuner_IF_Freq;
    ifOutCfg.ifInversion = MXL_DISABLE;
    ifOutCfg.gainLevel = 11;
    ifOutCfg.manualFreqSet = MXL_DISABLE;
    ifOutCfg.manualIFOutFreqInKHz = 0;
    status = MxLWare603_API_CfgTunerIFOutParam(tuner_id, ifOutCfg);
    if (status != MXL_SUCCESS)
    {
        TUN_MXL603_PRINTF("Error! MxLWare603_API_CfgTunerIFOutParam\n");    
        return status;
    }

    //Step 5 : AGC Setting
    agcCfg.agcType = MXL603_AGC_EXTERNAL;
//    agcCfg.agcType = MXL603_AGC_SELF;
    agcCfg.setPoint = 66;
    agcCfg.agcPolarityInverstion = MXL_DISABLE;
    status = MxLWare603_API_CfgTunerAGC(tuner_id, agcCfg);
    if (status != MXL_SUCCESS)
    {
        TUN_MXL603_PRINTF("Error! MxLWare603_API_CfgTunerAGC\n");    
        return status;
    }

    //Step 6 : Application Mode setting
    tunerModeCfg.signalMode = m_SpecialConfig[tuner_id].signalMode;
    tunerModeCfg.ifOutFreqinKHz = MxL603_Config[tuner_id].wTuner_IF_Freq;   //4100
    tunerModeCfg.xtalFreqSel = MxL603_Config[tuner_id].cTuner_Crystal;
    tunerModeCfg.ifOutGainLevel = 11;
    status = MxLWare603_API_CfgTunerMode(tuner_id, tunerModeCfg);
    if (status != MXL_SUCCESS)
    {
        TUN_MXL603_PRINTF("Error! MxLWare603_API_CfgTunerMode\n");    
        return status;
    }

    //Step 7 : Channel frequency & bandwidth setting
    chanTuneCfg.bandWidth = MXL603_TERR_BW_8MHz;
    chanTuneCfg.freqInHz = 474000000;
    chanTuneCfg.signalMode = m_SpecialConfig[tuner_id].signalMode;
    chanTuneCfg.xtalFreqSel = MxL603_Config[tuner_id].cTuner_Crystal;
    chanTuneCfg.startTune = MXL_START_TUNE;
    status = MxLWare603_API_CfgTunerChanTune(tuner_id, chanTuneCfg);
    if (status != MXL_SUCCESS)
    {
        TUN_MXL603_PRINTF("Error! MxLWare603_API_CfgTunerChanTune\n");    
        return status;
    }
    
    // MxLWare603_API_CfgTunerChanTune():MxLWare603_OEM_WriteRegister(devId, START_TUNE_REG, 0x01) will make singleSupply_3_3V mode become MXL_DISABLE,
    // It cause the power becomes 1.2V.
    //So it must be reset singleSupply_3_3V mode.
//    if (MXL_ENABLE == singleSupply_3_3V)
//      status |= MxLWare603_OEM_WriteRegister(tuner_id, MAIN_REG_AMP, 0x14);

    // Wait 15 ms 
    MxLWare603_OEM_Sleep(15);

    status = MxLWare603_API_CfgTunerLoopThrough(tuner_id, MXL_ENABLE);
    if (status != MXL_SUCCESS)
	{
		return status;
	}

    return MxL_OK;
}


static MxL_ERR_MSG MxL603_Tuner_RFTune(UINT32 tuner_id, UINT32 RF_Freq_Hz, MXL603_BW_E BWMHz)
{
    MXL_STATUS status; 
    MXL603_CHAN_TUNE_CFG_T chanTuneCfg;

    chanTuneCfg.bandWidth = BWMHz;
    chanTuneCfg.freqInHz = RF_Freq_Hz;
    chanTuneCfg.signalMode = m_SpecialConfig[tuner_id].signalMode;
    chanTuneCfg.xtalFreqSel = MxL603_Config[tuner_id].cTuner_Crystal;;
    chanTuneCfg.startTune = MXL_START_TUNE;
    status = MxLWare603_API_CfgTunerChanTune(tuner_id, chanTuneCfg);
    if (status != MXL_SUCCESS)
    {
        TUN_MXL603_PRINTF("Error! MxLWare603_API_CfgTunerChanTune\n");    
        return status;
    }

    // Wait 15 ms 
    MxLWare603_OEM_Sleep(15);

	return MxL_OK;
}




INT32 tun_mxl603_powcontrol(UINT32 tuner_id, UINT8 stdby)
{
    MXL_STATUS status;

	if(tuner_id >= tuner_cnt)
    {
		return ERR_FAILUE;
    }

	if (stdby)
	{
		//TUN_MXL603_PRINTF("start standby mode!\n");
		status = MxLWare603_API_CfgDevPowerMode(tuner_id, MXL603_PWR_MODE_STANDBY);
		if (status != MXL_SUCCESS)
		{
			//TUN_MXL603_PRINTF("standby mode setting fail!\n");
			return ERR_FAILUE;
		}
	}
	else
	{
		//TUN_MXL603_PRINTF("start wakeup mode!\n");
		status = MxLWare603_API_CfgDevPowerMode(tuner_id, MXL603_PWR_MODE_ACTIVE);
		if (status != MXL_SUCCESS)
		{
			//TUN_MXL603_PRINTF("wakeup mode setting fail!\n");
			return ERR_FAILUE;
		}
    
//    	if( MxL603_Tuner_Init(tuner_id) != MxL_OK )
//    		return ERR_FAILUE;
	}

	return SUCCESS;
}

INT32 tun_mxl603_command(UINT32 tuner_id, INT32 cmd, UINT32 param)
{
    INT32 ret = SUCCESS;
	
#if 0
	if(tuner_id >= tuner_cnt)
	{
		return ERR_FAILUE;
	}
	if ( bMxl_Tuner_Inited[tuner_id] == FALSE )
	{
		if(MxL603_Tuner_Init(tuner_id) != MxL_OK )
		{
			return ERR_FAILUE;
		}
		bMxl_Tuner_Inited[tuner_id] = TRUE;
	}
#endif

    switch(cmd)
    {
        case NIM_TUNER_SET_THROUGH_MODE:
            ret = set_through_mode(tuner_id, (DEM_WRITE_READ_TUNER *)param);
            break;

        case NIM_TUNER_POWER_CONTROL:
            tun_mxl603_powcontrol(tuner_id, param);
            break;

		case NIM_TUNER_GET_AGC:
			ret = tun_mxl603_get_rf_level(tuner_id, (UINT8 *)param);
			break;

		case NIM_TUNER_GET_RF_POWER_LEVEL:
			ret = MxLWare603_API_ReqTunerRxPower(tuner_id, (REAL32*)param);
			break;

        default:
            ret = ERR_FAILUE;
            break;
    }

    return ret;
}


#endif

